Repository navigation
Fix concurrent async websocket use: single reader, per-request queues - #249
Merged
Merged
Conversation
Replace the racing-recv() polling loop with a single background reader task that is the only thing allowed to touch the socket. recv() calls now wait on a shared Condition and get notified per dispatched message, instead of each concurrently calling _async_recv() and starving each other. Errors are routed to the specific msg_id they belong to when attributable, and broadcast to all waiters only when the connection itself is dead. Enable nimax id-aware WebSocket replay gating so test cassettes correctly model concurrent, out-of-order request/response correlation instead of relying on positional ordering.
adamlogan73
force-pushed
the
fix/async-reader-dispatch
branch
from
December 3, 2026 05:47
2dae682 to
4f6d406
Compare
Replace the shared Condition (which woke every waiter on every message) with one asyncio.Queue per pending request id. Responses, events and errors land only in their owner's queue, in arrival order. Storage moves behind _store_result/_store_event/_store_pong hooks so the sync client keeps its dicts. Also fixes: replies arriving before send() returned were lost; per-request state leaked; recv() hung after the client closed or after a cancelled listen_* body; the reader kept reading after a protocol error.
Record the PR #248 scenario against a live Home Assistant: two event listeners and a ping share one connection, and each listener receives only its own events, in order. Also cover a malformed event leaving its subscription alive, and a failed send releasing its request state.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Alternative to #248. Fixes concurrent use of
AsyncWebsocketClient: several listeners, pings and one-shot commands on one connection.recv()calls no longer race each other's_async_recv(), so they can't split a fragmented message or starve each other.asyncio.Queue, created insend()before the payload goes out. Results, events and errors go only to their owner's queue, in arrival order. A subscription's result therefore always comes before its events, and a message wakes only the task waiting for it. This replaces a shared Condition that woke every waiter on every message.recv(), and the reader stops after a protocol error.listen_*body raises. Late events for a cancelled subscription are dropped.basewebsocket.pystill parses messages by type, then hands them to_store_result/_store_event/_store_pong. The syncWebsocketClientimplements those with its existing dicts, so its behavior is unchanged.No public API changes. One edge case behaves differently:
recv(msg_id)for an id with no pending request now raisesReceivingErrorinstead of waiting forever.Related
Test plan
send()returns, routing by id, error isolation, fatal error fan-out, closing with pending waiters, cancellation and timeout cleanup, a malformed event on a subscription, a failed senduv run pytest: 191 passed, plus ty, ruff and pre-commit🤖 Generated with Claude Code